//
//  WebViewController.m
//  Tour
//
//  Created by 才诗琦 on 16/3/11.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "WebViewController.h"
#import "PrefixHeader.pch"
#import <WebKit/WebKit.h>

#import <MapKit/MapKit.h>
@interface WebViewController ()<UIWebViewDelegate>
@property(nonatomic, strong)NSString *webTitle;
@property(nonatomic,strong)CLGeocoder *geocoder;

@end

@implementation WebViewController
{
    MBProgressHUD *HUD;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    

    self.view.backgroundColor = [UIColor whiteColor];
    /**< 导航栏透明 */
    //     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"male"] forBarMetrics:UIBarMetricsCompactPrompt];
    
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];//导航条返回键带的首页 让他消失
    
    //地图按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"9635"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
    
    
    /**< 设置导航栏标题 */
    self.navigationItem.title = @"详情";
    
    UIWebView *wv = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT +400)];
    
    wv.scrollView.bounces = NO;
    /**< 自适应边界 */
    self.automaticallyAdjustsScrollViewInsets = YES;
    [self.view addSubview:wv];

    
    [self showProgressDialog];
    
    // js
    wv.scalesPageToFit = YES;
    wv.delegate = self;
    NSURL *url = [[NSURL alloc]initWithString:self.urlstr];
    NSURLRequest *request =  [[NSURLRequest alloc] initWithURL:url];
    [wv loadRequest:request];
   
    
    self.navigationItem.rightBarButtonItem.enabled = NO;

}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        return NO;
    }else{
        return YES;
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{

    [HUD hide:YES];
    self.navigationItem.rightBarButtonItem.enabled = YES;
    self.webTitle = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

}
#pragma mark - 菊花类

- (void)showProgressDialog
{
    
    //初始化进度框，置于当前的View当中
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    
    //如果设置此属性则当前的view置于后台
    HUD.dimBackground = YES;
    
    //设置对话框文字
    HUD.labelText = @"请稍等";
    
    //显示对话框
    [HUD showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(5);
    } completionBlock:^{
        //操作执行完后取消对话框
        [HUD removeFromSuperview];

        HUD = nil;
    }];
}


#pragma - mark 地图方法实现
- (void)right:(UIBarButtonItem *)right
{
    self.hidesBottomBarWhenPushed = YES;
//    MapViewController *map = [[MapViewController alloc] init];
//    map.str = self.str;
//    [self.navigationController pushViewController:map animated:YES];
//
    _geocoder = [[CLGeocoder alloc] init];
    [self listPlacemark];
    
    
    self.hidesBottomBarWhenPushed = NO;
}
#pragma mark 在地图上定位
-(void)location{
    //根据“北京市”进行地理编码
    [_geocoder geocodeAddressString:self.str completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *clPlacemark=[placemarks firstObject];//获取第一个地标
        MKPlacemark *mkplacemark=[[MKPlacemark alloc]initWithPlacemark:clPlacemark];//定位地标转化为地图的地标
        NSDictionary *options=@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard)};
        MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:mkplacemark];
        [mapItem openInMapsWithLaunchOptions:options];
    }];
}
-(void)listPlacemark{
    //根据“北京市”进行地理编码
    [_geocoder geocodeAddressString:self.str completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *clPlacemark1=[placemarks firstObject];//获取第一个地标
        MKPlacemark *mkPlacemark1=[[MKPlacemark alloc]initWithPlacemark:clPlacemark1];
        //注意地理编码一次只能定位到一个位置，不能同时定位，所在放到第一个位置定位完成回调函数中再次定位
        [_geocoder geocodeAddressString:self.str completionHandler:^(NSArray *placemarks, NSError *error) {
            CLPlacemark *clPlacemark2=[placemarks firstObject];//获取第一个地标
            MKPlacemark *mkPlacemark2=[[MKPlacemark alloc]initWithPlacemark:clPlacemark2];
            NSDictionary *options=@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard)};
            //MKMapItem *mapItem1=[MKMapItem mapItemForCurrentLocation];//当前位置
            MKMapItem *mapItem1=[[MKMapItem alloc]initWithPlacemark:mkPlacemark1];
            MKMapItem *mapItem2=[[MKMapItem alloc]initWithPlacemark:mkPlacemark2];
            [MKMapItem openMapsWithItems:@[mapItem1,mapItem2] launchOptions:options];
            
        }];
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
